package cn.com.homeyoda.aimate.aihealth.ui.my.questionnaire;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.databinding.library.baseAdapters.BR;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import cn.com.homeyoda.aimate.aihealth.R;
import cn.com.homeyoda.aimate.aihealth.app.AppViewModelFactory;
import cn.com.homeyoda.aimate.aihealth.base.BaseFragment;
import cn.com.homeyoda.aimate.aihealth.bean.QuestionnaireBean;
import cn.com.homeyoda.aimate.aihealth.databinding.FragmentMyChildBinding;
import cn.com.homeyoda.aimate.aihealth.ui.my.MyViewModel;
import cn.com.homeyoda.aimate.aihealth.ui.questionnaire.QuestionnaireCoverActivity;

public class QuestionnaireFragment extends BaseFragment<FragmentMyChildBinding, MyViewModel> {

    private QuestionnaireAdapter questionnaireAdapter;

    @Override
    public MyViewModel initViewModel() {
        AppViewModelFactory factory = AppViewModelFactory.getInstance(getActivity().getApplication());
        return ViewModelProviders.of(this, factory).get(MyViewModel.class);
    }

    @Override
    public void initData() {
        super.initData();
        viewModel.getQuestionnaireData();
        binding.tvTitle.setText(getString(R.string.tab_wdwj));
        initRecyclerView();
    }

    private void initRecyclerView() {
        questionnaireAdapter = new QuestionnaireAdapter(R.layout.questionnaire_item);
        binding.setLayoutManager(new LinearLayoutManager(getActivity()));
        DividerItemDecoration divider = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(getContext(), R.drawable.divider_transparent));
        binding.rvMyContent.addItemDecoration(divider);
        binding.setAdapter(questionnaireAdapter);
        questionnaireAdapter.setOnItemChildClickListener((adapter, view, position) -> {
                    QuestionnaireBean bean = (QuestionnaireBean) adapter.getItem(position);
                    if (bean == null) return;
                    QuestionnaireCoverActivity.start(getActivity(), bean.getId(), bean.getTitle());
                }
        );
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();
        viewModel.questionnaireData.observe(this, list -> questionnaireAdapter.setNewData(list));
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.fragment_my_child;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }
}
